﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data.Linq;
using Microsoft.MediaCenter.UI;
using MyMoviesBrowserLibrary;

namespace MyMoviesBrowser
{
    public class MainMenu : ContentPage
    {
        MovieMenu movieMenu;
        TrailerMenu trailerMenu;
        TVMenu tVMenu;

        public MainMenu()
        {
            Content = new VirtualList(this, null);

            AddCommand("Movies", null);
            AddCommand("Trailers", null);
            AddCommand("TV Shows", null);

            movieMenu = new MovieMenu(null);
            trailerMenu = new TrailerMenu(null);
            tVMenu = new TVMenu(null);
        }

        void command_Invoked(object sender, EventArgs e)
        {
            MenuCommand command = sender as MenuCommand;
            switch (command.Description)
            {
                case "Movies":
                    Application.Current.GoToMovieMenu(movieMenu);
                    break;
                case "Trailers":
                    Application.Current.GoToTrailerMenu(trailerMenu);
                    break;
                case "TV Shows":
                    Application.Current.GoToTVMenu(tVMenu);
                    break;
                default:
                    break;
            }
        }

        private void AddCommand(string desc, object data)
        {
            MenuCommand command = new MenuCommand() { Description = desc, Owner = this, Data = data };
            command.Invoked += new EventHandler(command_Invoked);
            Content.Add(command);
        }

        public override int JumpTo(string displayValue)
        {
            displayValue = displayValue.ToLower();
            List<string> commands = Content.Cast<MenuCommand>().Select(c => c.Description).ToList();
            List<string> temp = new List<string>();
            int x = -1;
            do
            {
                for (int i = 0; i < commands.Count; i++)
                {
                    if (commands[i].ToLower().StartsWith(displayValue))
                    {
                        x = i;
                        break;
                    }
                }

                if (displayValue.Length > 0)
                    displayValue = displayValue.Substring(0, displayValue.Length - 1);
            } while (x < 0 && displayValue.Length > 0);

            if (x >= 0)
                return x;
            return -1;
        }
    }
}
